{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-06-26T08:23:35.594598Z",
     "start_time": "2024-06-26T08:23:21.886172Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "df = pd.read_excel('./group_anlysis.xlsx')"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-26T08:23:35.654875Z",
     "start_time": "2024-06-26T08:23:35.595107Z"
    }
   },
   "cell_type": "code",
   "source": "df.info()",
   "id": "29641cbd84103a57",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 289386 entries, 0 to 289385\n",
      "Data columns (total 4 columns):\n",
      " #   Column  Non-Null Count   Dtype \n",
      "---  ------  --------------   ----- \n",
      " 0   主订单编号   289386 non-null  int64 \n",
      " 1   用户ID    289386 non-null  object\n",
      " 2   付款时间    289386 non-null  object\n",
      " 3   实付金额    289386 non-null  int64 \n",
      "dtypes: int64(2), object(2)\n",
      "memory usage: 8.8+ MB\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:06:16.659669Z",
     "start_time": "2024-06-25T02:06:16.594365Z"
    }
   },
   "cell_type": "code",
   "source": "df.head()",
   "id": "51c79161ec8a3891",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         主订单编号          用户ID                 付款时间  实付金额\n",
       "0  73465136654  uid135460366  2023-01-01 09:32:12   166\n",
       "1  73465136655  uid135460367  2023-01-01 09:11:50   117\n",
       "2  73465136656  uid135460368  2023-01-01 11:49:02   166\n",
       "3  73465136657  uid135460369  2023-01-01 12:20:24    77\n",
       "4  73465136658  uid135460370  2023-01-01 01:23:15   158"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>主订单编号</th>\n",
       "      <th>用户ID</th>\n",
       "      <th>付款时间</th>\n",
       "      <th>实付金额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>73465136654</td>\n",
       "      <td>uid135460366</td>\n",
       "      <td>2023-01-01 09:32:12</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>73465136655</td>\n",
       "      <td>uid135460367</td>\n",
       "      <td>2023-01-01 09:11:50</td>\n",
       "      <td>117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>73465136656</td>\n",
       "      <td>uid135460368</td>\n",
       "      <td>2023-01-01 11:49:02</td>\n",
       "      <td>166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>73465136657</td>\n",
       "      <td>uid135460369</td>\n",
       "      <td>2023-01-01 12:20:24</td>\n",
       "      <td>77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>73465136658</td>\n",
       "      <td>uid135460370</td>\n",
       "      <td>2023-01-01 01:23:15</td>\n",
       "      <td>158</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:06:43.624006Z",
     "start_time": "2024-06-25T02:06:43.599805Z"
    }
   },
   "cell_type": "code",
   "source": "df.describe()",
   "id": "7ea2733680efd566",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "              主订单编号           实付金额\n",
       "count  2.893860e+05  289386.000000\n",
       "mean   7.346531e+10     157.228857\n",
       "std    1.022321e+05      98.872567\n",
       "min    7.346514e+10      40.000000\n",
       "25%    7.346522e+10      77.000000\n",
       "50%    7.346530e+10     143.000000\n",
       "75%    7.346540e+10     196.000000\n",
       "max    7.346549e+10     581.000000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>主订单编号</th>\n",
       "      <th>实付金额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>2.893860e+05</td>\n",
       "      <td>289386.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>7.346531e+10</td>\n",
       "      <td>157.228857</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>1.022321e+05</td>\n",
       "      <td>98.872567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>7.346514e+10</td>\n",
       "      <td>40.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.346522e+10</td>\n",
       "      <td>77.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>7.346530e+10</td>\n",
       "      <td>143.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>7.346540e+10</td>\n",
       "      <td>196.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>7.346549e+10</td>\n",
       "      <td>581.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:09:08.647163Z",
     "start_time": "2024-06-25T02:09:08.551046Z"
    }
   },
   "cell_type": "code",
   "source": "df['年月标签'] = df['付款时间'].str[:7] # 字符串截取, 获取年月字符串数据",
   "id": "a89a0af95276c0bf",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:10:15.269574Z",
     "start_time": "2024-06-25T02:10:15.244209Z"
    }
   },
   "cell_type": "code",
   "source": "df['年月标签'].value_counts().sort_index()",
   "id": "3f8de0e9f8031a9f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2023-01    12039\n",
       "2023-02     4114\n",
       "2023-03    18172\n",
       "2023-04    12320\n",
       "2023-05    15738\n",
       "2023-06    44265\n",
       "2023-07    16102\n",
       "2023-08    28817\n",
       "2023-09    60376\n",
       "2023-10    21639\n",
       "2023-11    40204\n",
       "2023-12    15600\n",
       "Name: 年月标签, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "- 每个月要知道当前月份的新增用户  如果我们有用户注册的表, 知道了用户的注册时间, 直接可以计算了\n",
    "- 在这里我们把用户的首次购买作为 新增的标志\n",
    "- 计算当前月份的新增用户, 在后面的月份是否有购买  有购买算复购 "
   ],
   "id": "2dbe02b0f892d4b3"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 以2023年2月数据为例, 先算出一个月的数据来, 再for循环计算其它月份的",
   "id": "668287d9bec0012f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:15:17.423918Z",
     "start_time": "2024-06-25T02:15:17.361803Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from pandas import DataFrame\n",
    "\n",
    "month='2023-02'\n",
    "sample:DataFrame=df.loc[df['年月标签']==month]\n",
    "sample"
   ],
   "id": "f7bdeb4b8466a206",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             主订单编号          用户ID                 付款时间  实付金额     年月标签\n",
       "1889   73465138785  uid135460903  2023-02-01 11:27:36   174  2023-02\n",
       "1890   73465138786  uid135461902  2023-02-01 12:22:35   196  2023-02\n",
       "1915   73465138814  uid135461924  2023-02-02 20:24:53   235  2023-02\n",
       "1921   73465138820  uid135461927  2023-02-03 11:35:04   235  2023-02\n",
       "1960   73465138865  uid135460481  2023-02-05 13:49:07   193  2023-02\n",
       "...            ...           ...                  ...   ...      ...\n",
       "39007  73465180827  uid135478560  2023-02-28 19:16:15    43  2023-02\n",
       "39008  73465180828  uid135479071  2023-02-28 19:22:05   146  2023-02\n",
       "39009  73465180829  uid135479071  2023-02-28 19:22:05   308  2023-02\n",
       "39010  73465180830  uid135483504  2023-02-28 23:06:16    77  2023-02\n",
       "39022  73465180843  uid135483510  2023-02-28 23:32:44    43  2023-02\n",
       "\n",
       "[4114 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>主订单编号</th>\n",
       "      <th>用户ID</th>\n",
       "      <th>付款时间</th>\n",
       "      <th>实付金额</th>\n",
       "      <th>年月标签</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1889</th>\n",
       "      <td>73465138785</td>\n",
       "      <td>uid135460903</td>\n",
       "      <td>2023-02-01 11:27:36</td>\n",
       "      <td>174</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1890</th>\n",
       "      <td>73465138786</td>\n",
       "      <td>uid135461902</td>\n",
       "      <td>2023-02-01 12:22:35</td>\n",
       "      <td>196</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1915</th>\n",
       "      <td>73465138814</td>\n",
       "      <td>uid135461924</td>\n",
       "      <td>2023-02-02 20:24:53</td>\n",
       "      <td>235</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1921</th>\n",
       "      <td>73465138820</td>\n",
       "      <td>uid135461927</td>\n",
       "      <td>2023-02-03 11:35:04</td>\n",
       "      <td>235</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1960</th>\n",
       "      <td>73465138865</td>\n",
       "      <td>uid135460481</td>\n",
       "      <td>2023-02-05 13:49:07</td>\n",
       "      <td>193</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39007</th>\n",
       "      <td>73465180827</td>\n",
       "      <td>uid135478560</td>\n",
       "      <td>2023-02-28 19:16:15</td>\n",
       "      <td>43</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39008</th>\n",
       "      <td>73465180828</td>\n",
       "      <td>uid135479071</td>\n",
       "      <td>2023-02-28 19:22:05</td>\n",
       "      <td>146</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39009</th>\n",
       "      <td>73465180829</td>\n",
       "      <td>uid135479071</td>\n",
       "      <td>2023-02-28 19:22:05</td>\n",
       "      <td>308</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39010</th>\n",
       "      <td>73465180830</td>\n",
       "      <td>uid135483504</td>\n",
       "      <td>2023-02-28 23:06:16</td>\n",
       "      <td>77</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39022</th>\n",
       "      <td>73465180843</td>\n",
       "      <td>uid135483510</td>\n",
       "      <td>2023-02-28 23:32:44</td>\n",
       "      <td>43</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4114 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:21:08.735597Z",
     "start_time": "2024-06-25T02:21:08.729596Z"
    }
   },
   "cell_type": "code",
   "source": "sample.shape",
   "id": "e97cf1060393c7e4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4114, 5)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:21:32.090960Z",
     "start_time": "2024-06-25T02:21:32.084407Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 从2月的销售流水中去重得到所有2月的用户ID的唯一值\n",
    "sample_unique = sample.drop_duplicates(subset=['用户ID'])"
   ],
   "id": "f1a934ee2f7642d",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:22:36.674315Z",
     "start_time": "2024-06-25T02:22:36.644230Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取1月的数据\n",
    "history_df = df.loc[df['年月标签']=='2023-01'] \n",
    "# 判断2月的用户是否在1月的用户数据中, 如果在数据中说明是1月的留存(复购)用户, 如果不在1月的用户数据中, 说明是2月的新增用户\n",
    "sample_unique['用户ID'].isin(history_df['用户ID'])"
   ],
   "id": "41188c0ac97bb19d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1889      True\n",
       "1890     False\n",
       "1915     False\n",
       "1921     False\n",
       "1960      True\n",
       "         ...  \n",
       "39006    False\n",
       "39007     True\n",
       "39008     True\n",
       "39010    False\n",
       "39022    False\n",
       "Name: 用户ID, Length: 3313, dtype: bool"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:25:29.546323Z",
     "start_time": "2024-06-25T02:25:29.540325Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# ~ 取反的符号  True →False  False →True\n",
    "# 对在1月出现的ID范围内的数据取反 得到的就是不在这个范围的, 就是2月的新用户\n",
    "sample_unique_new = sample_unique.loc[~(sample_unique['用户ID'].isin(history_df['用户ID']))]"
   ],
   "id": "b7ac1b98f4076f56",
   "outputs": [],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:25:32.698797Z",
     "start_time": "2024-06-25T02:25:32.688339Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二月的新增用户\n",
    "sample_unique_new"
   ],
   "id": "d2f757228809a1f9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "             主订单编号          用户ID                 付款时间  实付金额     年月标签\n",
       "1890   73465138786  uid135461902  2023-02-01 12:22:35   196  2023-02\n",
       "1915   73465138814  uid135461924  2023-02-02 20:24:53   235  2023-02\n",
       "1921   73465138820  uid135461927  2023-02-03 11:35:04   235  2023-02\n",
       "1961   73465138866  uid135461961  2023-02-05 15:35:28   193  2023-02\n",
       "1972   73465138878  uid135461971  2023-02-06 12:06:45   174  2023-02\n",
       "...            ...           ...                  ...   ...      ...\n",
       "39004  73465180824  uid135483501  2023-02-28 18:39:23   373  2023-02\n",
       "39005  73465180825  uid135483502  2023-02-28 18:52:44   148  2023-02\n",
       "39006  73465180826  uid135483503  2023-02-28 22:37:59    43  2023-02\n",
       "39010  73465180830  uid135483504  2023-02-28 23:06:16    77  2023-02\n",
       "39022  73465180843  uid135483510  2023-02-28 23:32:44    43  2023-02\n",
       "\n",
       "[2740 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>主订单编号</th>\n",
       "      <th>用户ID</th>\n",
       "      <th>付款时间</th>\n",
       "      <th>实付金额</th>\n",
       "      <th>年月标签</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1890</th>\n",
       "      <td>73465138786</td>\n",
       "      <td>uid135461902</td>\n",
       "      <td>2023-02-01 12:22:35</td>\n",
       "      <td>196</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1915</th>\n",
       "      <td>73465138814</td>\n",
       "      <td>uid135461924</td>\n",
       "      <td>2023-02-02 20:24:53</td>\n",
       "      <td>235</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1921</th>\n",
       "      <td>73465138820</td>\n",
       "      <td>uid135461927</td>\n",
       "      <td>2023-02-03 11:35:04</td>\n",
       "      <td>235</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1961</th>\n",
       "      <td>73465138866</td>\n",
       "      <td>uid135461961</td>\n",
       "      <td>2023-02-05 15:35:28</td>\n",
       "      <td>193</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1972</th>\n",
       "      <td>73465138878</td>\n",
       "      <td>uid135461971</td>\n",
       "      <td>2023-02-06 12:06:45</td>\n",
       "      <td>174</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39004</th>\n",
       "      <td>73465180824</td>\n",
       "      <td>uid135483501</td>\n",
       "      <td>2023-02-28 18:39:23</td>\n",
       "      <td>373</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39005</th>\n",
       "      <td>73465180825</td>\n",
       "      <td>uid135483502</td>\n",
       "      <td>2023-02-28 18:52:44</td>\n",
       "      <td>148</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39006</th>\n",
       "      <td>73465180826</td>\n",
       "      <td>uid135483503</td>\n",
       "      <td>2023-02-28 22:37:59</td>\n",
       "      <td>43</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39010</th>\n",
       "      <td>73465180830</td>\n",
       "      <td>uid135483504</td>\n",
       "      <td>2023-02-28 23:06:16</td>\n",
       "      <td>77</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39022</th>\n",
       "      <td>73465180843</td>\n",
       "      <td>uid135483510</td>\n",
       "      <td>2023-02-28 23:32:44</td>\n",
       "      <td>43</td>\n",
       "      <td>2023-02</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2740 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:32:25.781434Z",
     "start_time": "2024-06-25T02:32:25.511441Z"
    }
   },
   "cell_type": "code",
   "source": [
    "month_list = df['年月标签'].unique().tolist()[2:]\n",
    "result_list = []\n",
    "for month in month_list:\n",
    "    # 取出一个月的数据 \n",
    "    next_month_df = df.loc[df['年月标签']==month]\n",
    "    # 新增用户的ID 出现在后面月份的数据中, 说明是复购用户\n",
    "    retention_users_df = sample_unique_new.loc[sample_unique_new['用户ID'].isin(next_month_df['用户ID'])]\n",
    "    # 把复购用户数量保存在列表中\n",
    "    result_list.append([month+'留存情况',retention_users_df.shape[0]])"
   ],
   "id": "129c58c110e90824",
   "outputs": [],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:32:30.430054Z",
     "start_time": "2024-06-25T02:32:30.423349Z"
    }
   },
   "cell_type": "code",
   "source": "result_list",
   "id": "d260cff8c6f86249",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['2023-03留存情况', 558],\n",
       " ['2023-04留存情况', 340],\n",
       " ['2023-05留存情况', 379],\n",
       " ['2023-06留存情况', 587],\n",
       " ['2023-07留存情况', 293],\n",
       " ['2023-08留存情况', 317],\n",
       " ['2023-09留存情况', 267],\n",
       " ['2023-10留存情况', 205],\n",
       " ['2023-11留存情况', 304],\n",
       " ['2023-12留存情况', 112]]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:33:47.222404Z",
     "start_time": "2024-06-25T02:33:47.218352Z"
    }
   },
   "cell_type": "code",
   "source": "result_list.insert(0,['2023年2月新增用户:',sample_unique_new.shape[0]])",
   "id": "fede5c5822c82eba",
   "outputs": [],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T02:33:52.720332Z",
     "start_time": "2024-06-25T02:33:52.714957Z"
    }
   },
   "cell_type": "code",
   "source": "result_list",
   "id": "c54d625a477889a6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['2023年2月新增用户:', 2740],\n",
       " ['2023-03留存情况', 558],\n",
       " ['2023-04留存情况', 340],\n",
       " ['2023-05留存情况', 379],\n",
       " ['2023-06留存情况', 587],\n",
       " ['2023-07留存情况', 293],\n",
       " ['2023-08留存情况', 317],\n",
       " ['2023-09留存情况', 267],\n",
       " ['2023-10留存情况', 205],\n",
       " ['2023-11留存情况', 304],\n",
       " ['2023-12留存情况', 112]]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T03:05:10.857769Z",
     "start_time": "2024-06-25T03:05:10.838665Z"
    }
   },
   "cell_type": "code",
   "source": "df['年月标签'].unique()",
   "id": "c83713932435501f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['2023-01', '2023-02', '2023-03', '2023-04', '2023-05', '2023-06',\n",
       "       '2023-07', '2023-08', '2023-09', '2023-10', '2023-11', '2023-12'],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T03:13:43.768392Z",
     "start_time": "2024-06-25T03:13:43.761173Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list1 = [1,2,3,4,5]\n",
    "list2= [6,7,8,9,10]\n",
    "list(zip(list1,list2))"
   ],
   "id": "1cbcf6843a06202f",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T03:19:46.599118Z",
     "start_time": "2024-06-25T03:19:46.590118Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list_0 = [0,0,0,0,0,0,0,0,0,0,0,0]\n",
    "list1 = [5,6,7,8,9,10,11]\n",
    "list2 = [1,2,3,4,5,6,7,8,9,10,11]\n",
    "list(zip(list1,list2))"
   ],
   "id": "d9ecb9fe38d658df",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(5, 1), (6, 2), (7, 3), (8, 4), (9, 5), (10, 6), (11, 7)]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T04:20:45.215541Z",
     "start_time": "2024-06-25T04:20:45.211415Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for j,cnt in zip(list1,list2):\n",
    "    print(j,cnt)"
   ],
   "id": "dbb3da0ddbf4f890",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 1\n",
      "6 2\n",
      "7 3\n",
      "8 4\n",
      "9 5\n",
      "10 6\n",
      "11 7\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T04:12:31.226854Z",
     "start_time": "2024-06-25T04:12:28.706885Z"
    }
   },
   "cell_type": "code",
   "source": [
    "month_list = df['年月标签'].unique().tolist() # 获取所有月份的列表\n",
    "final_df = pd.DataFrame() # 准备一个空白的df 用来保存最终的结果\n",
    "for i in range(len(month_list)-1): # 一共计算11个月\n",
    "    # 准备一个空白的列表, 用来保存当前月份计算的结果\n",
    "    count_list = [0]*len(month_list)\n",
    "    # 外层循环的目的是为了找到每个月的新增用户\n",
    "    # 先筛选当前月份的数据\n",
    "    target_month_df = df.loc[df['年月标签']==month_list[i]]\n",
    "    target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
    "    # 如果是第一个月, 不需要判断了,所有的用户都是新用户\n",
    "    if i==0:\n",
    "        new_users_df = target_month_df.copy()\n",
    "    else:\n",
    "        # 如果不是1月, 2月以后得数据, 需要先获取前面的所有月份的数据  month_list[:i]\n",
    "        history_df = df.loc[df['年月标签'].isin(month_list[:i])]\n",
    "        # 判断当前的用户是否在前面的月份出现过, 如果没有出现过 就留下来, 是当前月份的新用户\n",
    "        new_users_df = target_month_df.loc[(target_month_df['用户ID'].isin(history_df['用户ID']))==False]\n",
    "    \n",
    "    # 把新用户的数量保存到列表的第一个元素中\n",
    "    count_list[0]=new_users_df.shape[0]\n",
    "    #print(count_list)\n",
    "    # 内层循环, 用来计算新用户在后面月份的复购情况\n",
    "    for j,cnt in zip(range(i+1,len(month_list)),range(1,len(month_list))):\n",
    "        # j 用来循环后面的月份, 从i+1开始, i指向2月份, j就是3月份\n",
    "        # cnt 用来记录结果的 不管是哪个月份都是从1开始, 第0个元素记录的是新增用户\n",
    "        next_month_df = df.loc[df['年月标签']==month_list[j]]\n",
    "        # new_users_df['用户ID'].isin(next_month_df['用户ID']) 是True/False组成的列表 sum求和 计算的是True的数量\n",
    "        retention_count =(new_users_df['用户ID'].isin(next_month_df['用户ID'])).sum()\n",
    "        # 保存结果到列表\n",
    "        count_list[cnt] = retention_count\n",
    "    # 如果不是第一个月, 需要和历史的月份进行判断, 如果在历史月份中出现过,就不是新用户\n",
    "    # 要统计的是在前面的月份中,没有出现过的用户ID\n",
    "    result=pd.DataFrame({month_list[i]:count_list}).T\n",
    "    final_df = pd.concat([final_df,result])\n",
    "final_df.columns = ['当月新增','+1月','+2月','+3月','+4月','+5月','+6月','+7月','+8月','+9月','+10月','+11月']"
   ],
   "id": "f4611c993d0f9831",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\373218438.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T04:12:36.365524Z",
     "start_time": "2024-06-25T04:12:36.353540Z"
    }
   },
   "cell_type": "code",
   "source": "final_df",
   "id": "73699e5ad9ae1fa4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          当月新增   +1月   +2月   +3月   +4月   +5月  +6月   +7月   +8月  +9月  +10月  +11月\n",
       "2023-01   8193   573  1601  1050  1079  1906  815  1102   863  628  1049   372\n",
       "2023-02   2740   558   340   379   587   293  317   267   205  304   112     0\n",
       "2023-03   8753  1176  1232  2112   799  1032  777   616  1064  360     0     0\n",
       "2023-04   5859   828  1208   502   618   482  329   526   171    0     0     0\n",
       "2023-05   6912  1575   626   747   464   392  569   198     0    0     0     0\n",
       "2023-06  16458  1575  1775  1153   923  1482  496     0     0    0     0     0\n",
       "2023-07   6514   801   404   257   390   144    0     0     0    0     0     0\n",
       "2023-08  11781  1030   606   813   331     0    0     0     0    0     0     0\n",
       "2023-09  30214  2206  2482   971     0     0    0     0     0    0     0     0\n",
       "2023-10  11253  1147   452     0     0     0    0     0     0    0     0     0\n",
       "2023-11  18540   870     0     0     0     0    0     0     0    0     0     0"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>当月新增</th>\n",
       "      <th>+1月</th>\n",
       "      <th>+2月</th>\n",
       "      <th>+3月</th>\n",
       "      <th>+4月</th>\n",
       "      <th>+5月</th>\n",
       "      <th>+6月</th>\n",
       "      <th>+7月</th>\n",
       "      <th>+8月</th>\n",
       "      <th>+9月</th>\n",
       "      <th>+10月</th>\n",
       "      <th>+11月</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01</th>\n",
       "      <td>8193</td>\n",
       "      <td>573</td>\n",
       "      <td>1601</td>\n",
       "      <td>1050</td>\n",
       "      <td>1079</td>\n",
       "      <td>1906</td>\n",
       "      <td>815</td>\n",
       "      <td>1102</td>\n",
       "      <td>863</td>\n",
       "      <td>628</td>\n",
       "      <td>1049</td>\n",
       "      <td>372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-02</th>\n",
       "      <td>2740</td>\n",
       "      <td>558</td>\n",
       "      <td>340</td>\n",
       "      <td>379</td>\n",
       "      <td>587</td>\n",
       "      <td>293</td>\n",
       "      <td>317</td>\n",
       "      <td>267</td>\n",
       "      <td>205</td>\n",
       "      <td>304</td>\n",
       "      <td>112</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-03</th>\n",
       "      <td>8753</td>\n",
       "      <td>1176</td>\n",
       "      <td>1232</td>\n",
       "      <td>2112</td>\n",
       "      <td>799</td>\n",
       "      <td>1032</td>\n",
       "      <td>777</td>\n",
       "      <td>616</td>\n",
       "      <td>1064</td>\n",
       "      <td>360</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04</th>\n",
       "      <td>5859</td>\n",
       "      <td>828</td>\n",
       "      <td>1208</td>\n",
       "      <td>502</td>\n",
       "      <td>618</td>\n",
       "      <td>482</td>\n",
       "      <td>329</td>\n",
       "      <td>526</td>\n",
       "      <td>171</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-05</th>\n",
       "      <td>6912</td>\n",
       "      <td>1575</td>\n",
       "      <td>626</td>\n",
       "      <td>747</td>\n",
       "      <td>464</td>\n",
       "      <td>392</td>\n",
       "      <td>569</td>\n",
       "      <td>198</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06</th>\n",
       "      <td>16458</td>\n",
       "      <td>1575</td>\n",
       "      <td>1775</td>\n",
       "      <td>1153</td>\n",
       "      <td>923</td>\n",
       "      <td>1482</td>\n",
       "      <td>496</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07</th>\n",
       "      <td>6514</td>\n",
       "      <td>801</td>\n",
       "      <td>404</td>\n",
       "      <td>257</td>\n",
       "      <td>390</td>\n",
       "      <td>144</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-08</th>\n",
       "      <td>11781</td>\n",
       "      <td>1030</td>\n",
       "      <td>606</td>\n",
       "      <td>813</td>\n",
       "      <td>331</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-09</th>\n",
       "      <td>30214</td>\n",
       "      <td>2206</td>\n",
       "      <td>2482</td>\n",
       "      <td>971</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-10</th>\n",
       "      <td>11253</td>\n",
       "      <td>1147</td>\n",
       "      <td>452</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-11</th>\n",
       "      <td>18540</td>\n",
       "      <td>870</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:56:59.960191Z",
     "start_time": "2024-06-25T06:56:57.515015Z"
    }
   },
   "cell_type": "code",
   "source": [
    "month_list = df['年月标签'].unique().tolist() # 获取所有月份的列表\n",
    "final_df = pd.DataFrame() # 准备一个空白的df 用来保存最终的结果\n",
    "for i in range(len(month_list)-1): # 一共计算11个月\n",
    "    # 准备一个空白的列表, 用来保存当前月份计算的结果\n",
    "    count_list = []\n",
    "    # 外层循环的目的是为了找到每个月的新增用户\n",
    "    # 先筛选当前月份的数据\n",
    "    target_month_df = df.loc[df['年月标签']==month_list[i]]\n",
    "    target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
    "    # 如果是第一个月, 不需要判断了,所有的用户都是新用户\n",
    "    if i==0:\n",
    "        new_users_df = target_month_df.copy()\n",
    "    else:\n",
    "        # 如果不是1月, 2月以后得数据, 需要先获取前面的所有月份的数据  month_list[:i]\n",
    "        history_df = df.loc[df['年月标签'].isin(month_list[:i])]\n",
    "        # 判断当前的用户是否在前面的月份出现过, 如果没有出现过 就留下来, 是当前月份的新用户\n",
    "        new_users_df = target_month_df.loc[(target_month_df['用户ID'].isin(history_df['用户ID']))==False]\n",
    "    \n",
    "    # 把新用户的数量保存到列表里\n",
    "    count_list.append(new_users_df.shape[0])\n",
    "    #print(count_list)\n",
    "    # 内层循环, 用来计算新用户在后面月份的复购情况\n",
    "    for j in range(i+1,len(month_list)):\n",
    "        # j 用来循环后面的月份, 从i+1开始, i指向2月份, j就是3月份\n",
    "        # cnt 用来记录结果的 不管是哪个月份都是从1开始, 第0个元素记录的是新增用户\n",
    "        next_month_df = df.loc[df['年月标签']==month_list[j]]\n",
    "        # new_users_df['用户ID'].isin(next_month_df['用户ID']) 是True/False组成的列表 sum求和 计算的是True的数量\n",
    "        retention_count =(new_users_df['用户ID'].isin(next_month_df['用户ID'])).sum()\n",
    "        # 保存结果到列表\n",
    "        count_list.append(retention_count)\n",
    "    # 如果不是第一个月, 需要和历史的月份进行判断, 如果在历史月份中出现过,就不是新用户\n",
    "    # 要统计的是在前面的月份中,没有出现过的用户ID\n",
    "    result=pd.DataFrame({month_list[i]:count_list}).T\n",
    "    final_df = pd.concat([final_df,result])\n",
    "final_df.fillna(0,inplace=True)\n",
    "final_df.columns=['当月新增','+1月','+2月','+3月','+4月','+5月','+6月','+7月','+8月','+9月','+10月','+11月']"
   ],
   "id": "73350745d5d2b4ca",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\3755026837.py:9: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n"
     ]
    }
   ],
   "execution_count": 84
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:44:07.346134Z",
     "start_time": "2024-06-25T06:44:07.341210Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "c7d7efd093134f17",
   "outputs": [],
   "execution_count": 67
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:47:06.947Z",
     "start_time": "2024-06-25T06:47:06.941577Z"
    }
   },
   "cell_type": "code",
   "source": "result = final_df.divide(final_df['当月新增'],axis=0).iloc[:,1:]",
   "id": "3a795364a7da00dd",
   "outputs": [],
   "execution_count": 76
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:47:36.220848Z",
     "start_time": "2024-06-25T06:47:36.214936Z"
    }
   },
   "cell_type": "code",
   "source": "result.insert(loc=0,column='当月新增',value=final_df['当月新增'])",
   "id": "769ae6953c3d1059",
   "outputs": [],
   "execution_count": 77
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:47:40.044279Z",
     "start_time": "2024-06-25T06:47:40.029307Z"
    }
   },
   "cell_type": "code",
   "source": "result",
   "id": "f479fa5ef129a037",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          当月新增       +1月       +2月       +3月       +4月       +5月       +6月  \\\n",
       "2023-01   8193  0.069938  0.195411  0.128158  0.131698  0.232638  0.099475   \n",
       "2023-02   2740  0.203650  0.124088  0.138321  0.214234  0.106934  0.115693   \n",
       "2023-03   8753  0.134354  0.140752  0.241289  0.091283  0.117902  0.088770   \n",
       "2023-04   5859  0.141321  0.206179  0.085680  0.105479  0.082267  0.056153   \n",
       "2023-05   6912  0.227865  0.090567  0.108073  0.067130  0.056713  0.082321   \n",
       "2023-06  16458  0.095698  0.107850  0.070057  0.056082  0.090047  0.030137   \n",
       "2023-07   6514  0.122966  0.062020  0.039453  0.059871  0.022106  0.000000   \n",
       "2023-08  11781  0.087429  0.051439  0.069009  0.028096  0.000000  0.000000   \n",
       "2023-09  30214  0.073013  0.082147  0.032137  0.000000  0.000000  0.000000   \n",
       "2023-10  11253  0.101928  0.040167  0.000000  0.000000  0.000000  0.000000   \n",
       "2023-11  18540  0.046926  0.000000  0.000000  0.000000  0.000000  0.000000   \n",
       "\n",
       "              +7月       +8月       +9月      +10月      +11月  \n",
       "2023-01  0.134505  0.105334  0.076651  0.128036  0.045405  \n",
       "2023-02  0.097445  0.074818  0.110949  0.040876  0.000000  \n",
       "2023-03  0.070376  0.121558  0.041129  0.000000  0.000000  \n",
       "2023-04  0.089776  0.029186  0.000000  0.000000  0.000000  \n",
       "2023-05  0.028646  0.000000  0.000000  0.000000  0.000000  \n",
       "2023-06  0.000000  0.000000  0.000000  0.000000  0.000000  \n",
       "2023-07  0.000000  0.000000  0.000000  0.000000  0.000000  \n",
       "2023-08  0.000000  0.000000  0.000000  0.000000  0.000000  \n",
       "2023-09  0.000000  0.000000  0.000000  0.000000  0.000000  \n",
       "2023-10  0.000000  0.000000  0.000000  0.000000  0.000000  \n",
       "2023-11  0.000000  0.000000  0.000000  0.000000  0.000000  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>当月新增</th>\n",
       "      <th>+1月</th>\n",
       "      <th>+2月</th>\n",
       "      <th>+3月</th>\n",
       "      <th>+4月</th>\n",
       "      <th>+5月</th>\n",
       "      <th>+6月</th>\n",
       "      <th>+7月</th>\n",
       "      <th>+8月</th>\n",
       "      <th>+9月</th>\n",
       "      <th>+10月</th>\n",
       "      <th>+11月</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01</th>\n",
       "      <td>8193</td>\n",
       "      <td>0.069938</td>\n",
       "      <td>0.195411</td>\n",
       "      <td>0.128158</td>\n",
       "      <td>0.131698</td>\n",
       "      <td>0.232638</td>\n",
       "      <td>0.099475</td>\n",
       "      <td>0.134505</td>\n",
       "      <td>0.105334</td>\n",
       "      <td>0.076651</td>\n",
       "      <td>0.128036</td>\n",
       "      <td>0.045405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-02</th>\n",
       "      <td>2740</td>\n",
       "      <td>0.203650</td>\n",
       "      <td>0.124088</td>\n",
       "      <td>0.138321</td>\n",
       "      <td>0.214234</td>\n",
       "      <td>0.106934</td>\n",
       "      <td>0.115693</td>\n",
       "      <td>0.097445</td>\n",
       "      <td>0.074818</td>\n",
       "      <td>0.110949</td>\n",
       "      <td>0.040876</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-03</th>\n",
       "      <td>8753</td>\n",
       "      <td>0.134354</td>\n",
       "      <td>0.140752</td>\n",
       "      <td>0.241289</td>\n",
       "      <td>0.091283</td>\n",
       "      <td>0.117902</td>\n",
       "      <td>0.088770</td>\n",
       "      <td>0.070376</td>\n",
       "      <td>0.121558</td>\n",
       "      <td>0.041129</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04</th>\n",
       "      <td>5859</td>\n",
       "      <td>0.141321</td>\n",
       "      <td>0.206179</td>\n",
       "      <td>0.085680</td>\n",
       "      <td>0.105479</td>\n",
       "      <td>0.082267</td>\n",
       "      <td>0.056153</td>\n",
       "      <td>0.089776</td>\n",
       "      <td>0.029186</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-05</th>\n",
       "      <td>6912</td>\n",
       "      <td>0.227865</td>\n",
       "      <td>0.090567</td>\n",
       "      <td>0.108073</td>\n",
       "      <td>0.067130</td>\n",
       "      <td>0.056713</td>\n",
       "      <td>0.082321</td>\n",
       "      <td>0.028646</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06</th>\n",
       "      <td>16458</td>\n",
       "      <td>0.095698</td>\n",
       "      <td>0.107850</td>\n",
       "      <td>0.070057</td>\n",
       "      <td>0.056082</td>\n",
       "      <td>0.090047</td>\n",
       "      <td>0.030137</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07</th>\n",
       "      <td>6514</td>\n",
       "      <td>0.122966</td>\n",
       "      <td>0.062020</td>\n",
       "      <td>0.039453</td>\n",
       "      <td>0.059871</td>\n",
       "      <td>0.022106</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-08</th>\n",
       "      <td>11781</td>\n",
       "      <td>0.087429</td>\n",
       "      <td>0.051439</td>\n",
       "      <td>0.069009</td>\n",
       "      <td>0.028096</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-09</th>\n",
       "      <td>30214</td>\n",
       "      <td>0.073013</td>\n",
       "      <td>0.082147</td>\n",
       "      <td>0.032137</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-10</th>\n",
       "      <td>11253</td>\n",
       "      <td>0.101928</td>\n",
       "      <td>0.040167</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-11</th>\n",
       "      <td>18540</td>\n",
       "      <td>0.046926</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 78
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:58:24.433916Z",
     "start_time": "2024-06-25T06:58:21.897682Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 客单价 \n",
    "month_list = df['年月标签'].unique().tolist() # 获取所有月份的列表\n",
    "final_df_monetary = pd.DataFrame() # 准备一个空白的df 用来保存最终的结果\n",
    "for i in range(len(month_list)-1): # 一共计算11个月\n",
    "    # 准备一个空白的列表, 用来保存当前月份计算的结果\n",
    "    count_list = []\n",
    "    # 外层循环的目的是为了找到每个月的新增用户\n",
    "    # 先筛选当前月份的数据\n",
    "    target_month_df = df.loc[df['年月标签']==month_list[i]]\n",
    "    target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
    "    # 如果是第一个月, 不需要判断了,所有的用户都是新用户\n",
    "    if i==0:\n",
    "        new_users_df = target_month_df.copy()\n",
    "    else:\n",
    "        # 如果不是1月, 2月以后得数据, 需要先获取前面的所有月份的数据  month_list[:i]\n",
    "        history_df = df.loc[df['年月标签'].isin(month_list[:i])]\n",
    "        # 判断当前的用户是否在前面的月份出现过, 如果没有出现过 就留下来, 是当前月份的新用户\n",
    "        new_users_df = target_month_df.loc[(target_month_df['用户ID'].isin(history_df['用户ID']))==False]\n",
    "    \n",
    "    # 把新用户的数量保存到列表里\n",
    "    count_list.append(new_users_df.shape[0])\n",
    "    #print(count_list)\n",
    "    # 内层循环, 用来计算新用户在后面月份的复购情况\n",
    "    for j in range(i+1,len(month_list)):\n",
    "        # j 用来循环后面的月份, 从i+1开始, i指向2月份, j就是3月份\n",
    "        # cnt 用来记录结果的 不管是哪个月份都是从1开始, 第0个元素记录的是新增用户\n",
    "        next_month_df = df.loc[df['年月标签']==month_list[j]]\n",
    "        # new_users_df['用户ID'].isin(next_month_df['用户ID']) 是True/False组成的列表 sum求和 计算的是True的数量\n",
    "        # retention_count =(new_users_df['用户ID'].isin(next_month_df['用户ID'])).sum()\n",
    "        total_pay = next_month_df.loc[next_month_df['用户ID'].isin(new_users_df['用户ID']),'实付金额'].sum()\n",
    "        # 保存结果到列表\n",
    "        count_list.append(total_pay)\n",
    "    # 如果不是第一个月, 需要和历史的月份进行判断, 如果在历史月份中出现过,就不是新用户\n",
    "    # 要统计的是在前面的月份中,没有出现过的用户ID\n",
    "    result=pd.DataFrame({month_list[i]:count_list}).T\n",
    "    final_df_monetary = pd.concat([final_df_monetary,result])"
   ],
   "id": "355c98cc0eb74a7d",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_14652\\2265249913.py:10: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  target_month_df.drop_duplicates(subset=['用户ID'],inplace = True)\n"
     ]
    }
   ],
   "execution_count": 88
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:58:25.923943Z",
     "start_time": "2024-06-25T06:58:25.918867Z"
    }
   },
   "cell_type": "code",
   "source": [
    "final_df_monetary.fillna(0,inplace=True)\n",
    "final_df_monetary.columns =['当月新增','+1月','+2月','+3月','+4月','+5月','+6月','+7月','+8月','+9月','+10月','+11月']"
   ],
   "id": "40ef07c6428ffb60",
   "outputs": [],
   "execution_count": 89
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:58:27.777010Z",
     "start_time": "2024-06-25T06:58:27.752971Z"
    }
   },
   "cell_type": "code",
   "source": "final_df_monetary",
   "id": "906616480bfd663e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          当月新增     +1月       +2月       +3月       +4月       +5月       +6月  \\\n",
       "2023-01   8193  152038  474019.0  321004.0  301462.0  616853.0  185251.0   \n",
       "2023-02   2740  151328   94338.0  105943.0  189951.0   55792.0   77418.0   \n",
       "2023-03   8753  289928  284522.0  605839.0  162036.0  255507.0  228685.0   \n",
       "2023-04   5859  179413  323887.0  103464.0  143104.0  141508.0   85408.0   \n",
       "2023-05   6912  395880  128426.0  160008.0  123942.0   94661.0  146627.0   \n",
       "2023-06  16458  300293  383712.0  312063.0  233662.0  369007.0  108013.0   \n",
       "2023-07   6514  162437   97194.0   57777.0   84881.0   31884.0       0.0   \n",
       "2023-08  11781  276063  143451.0  186071.0   80948.0       0.0       0.0   \n",
       "2023-09  30214  555657  548818.0  277662.0       0.0       0.0       0.0   \n",
       "2023-10  11253  232308  124408.0       0.0       0.0       0.0       0.0   \n",
       "2023-11  18540  220271       0.0       0.0       0.0       0.0       0.0   \n",
       "\n",
       "              +7月       +8月       +9月      +10月     +11月  \n",
       "2023-01  292858.0  262933.0  180543.0  287445.0  93318.0  \n",
       "2023-02   77279.0   59372.0   78433.0   28066.0      0.0  \n",
       "2023-03  162398.0  275678.0   92286.0       0.0      0.0  \n",
       "2023-04  144100.0   41001.0       0.0       0.0      0.0  \n",
       "2023-05   46841.0       0.0       0.0       0.0      0.0  \n",
       "2023-06       0.0       0.0       0.0       0.0      0.0  \n",
       "2023-07       0.0       0.0       0.0       0.0      0.0  \n",
       "2023-08       0.0       0.0       0.0       0.0      0.0  \n",
       "2023-09       0.0       0.0       0.0       0.0      0.0  \n",
       "2023-10       0.0       0.0       0.0       0.0      0.0  \n",
       "2023-11       0.0       0.0       0.0       0.0      0.0  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>当月新增</th>\n",
       "      <th>+1月</th>\n",
       "      <th>+2月</th>\n",
       "      <th>+3月</th>\n",
       "      <th>+4月</th>\n",
       "      <th>+5月</th>\n",
       "      <th>+6月</th>\n",
       "      <th>+7月</th>\n",
       "      <th>+8月</th>\n",
       "      <th>+9月</th>\n",
       "      <th>+10月</th>\n",
       "      <th>+11月</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01</th>\n",
       "      <td>8193</td>\n",
       "      <td>152038</td>\n",
       "      <td>474019.0</td>\n",
       "      <td>321004.0</td>\n",
       "      <td>301462.0</td>\n",
       "      <td>616853.0</td>\n",
       "      <td>185251.0</td>\n",
       "      <td>292858.0</td>\n",
       "      <td>262933.0</td>\n",
       "      <td>180543.0</td>\n",
       "      <td>287445.0</td>\n",
       "      <td>93318.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-02</th>\n",
       "      <td>2740</td>\n",
       "      <td>151328</td>\n",
       "      <td>94338.0</td>\n",
       "      <td>105943.0</td>\n",
       "      <td>189951.0</td>\n",
       "      <td>55792.0</td>\n",
       "      <td>77418.0</td>\n",
       "      <td>77279.0</td>\n",
       "      <td>59372.0</td>\n",
       "      <td>78433.0</td>\n",
       "      <td>28066.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-03</th>\n",
       "      <td>8753</td>\n",
       "      <td>289928</td>\n",
       "      <td>284522.0</td>\n",
       "      <td>605839.0</td>\n",
       "      <td>162036.0</td>\n",
       "      <td>255507.0</td>\n",
       "      <td>228685.0</td>\n",
       "      <td>162398.0</td>\n",
       "      <td>275678.0</td>\n",
       "      <td>92286.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04</th>\n",
       "      <td>5859</td>\n",
       "      <td>179413</td>\n",
       "      <td>323887.0</td>\n",
       "      <td>103464.0</td>\n",
       "      <td>143104.0</td>\n",
       "      <td>141508.0</td>\n",
       "      <td>85408.0</td>\n",
       "      <td>144100.0</td>\n",
       "      <td>41001.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-05</th>\n",
       "      <td>6912</td>\n",
       "      <td>395880</td>\n",
       "      <td>128426.0</td>\n",
       "      <td>160008.0</td>\n",
       "      <td>123942.0</td>\n",
       "      <td>94661.0</td>\n",
       "      <td>146627.0</td>\n",
       "      <td>46841.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06</th>\n",
       "      <td>16458</td>\n",
       "      <td>300293</td>\n",
       "      <td>383712.0</td>\n",
       "      <td>312063.0</td>\n",
       "      <td>233662.0</td>\n",
       "      <td>369007.0</td>\n",
       "      <td>108013.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07</th>\n",
       "      <td>6514</td>\n",
       "      <td>162437</td>\n",
       "      <td>97194.0</td>\n",
       "      <td>57777.0</td>\n",
       "      <td>84881.0</td>\n",
       "      <td>31884.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-08</th>\n",
       "      <td>11781</td>\n",
       "      <td>276063</td>\n",
       "      <td>143451.0</td>\n",
       "      <td>186071.0</td>\n",
       "      <td>80948.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-09</th>\n",
       "      <td>30214</td>\n",
       "      <td>555657</td>\n",
       "      <td>548818.0</td>\n",
       "      <td>277662.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-10</th>\n",
       "      <td>11253</td>\n",
       "      <td>232308</td>\n",
       "      <td>124408.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-11</th>\n",
       "      <td>18540</td>\n",
       "      <td>220271</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:58:36.913160Z",
     "start_time": "2024-06-25T06:58:36.897281Z"
    }
   },
   "cell_type": "code",
   "source": "final_df_monetary/final_df",
   "id": "86d7b823a02849df",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "         当月新增         +1月         +2月         +3月         +4月         +5月  \\\n",
       "2023-01   1.0  265.336824  296.076827  305.718095  279.390176  323.637461   \n",
       "2023-02   1.0  271.197133  277.464706  279.532982  323.596252  190.416382   \n",
       "2023-03   1.0  246.537415  230.943182  286.855587  202.798498  247.584302   \n",
       "2023-04   1.0  216.682367  268.118377  206.103586  231.559871  293.585062   \n",
       "2023-05   1.0  251.352381  205.153355  214.200803  267.116379  241.482143   \n",
       "2023-06   1.0  190.662222  216.175775  270.653079  253.154930  248.992578   \n",
       "2023-07   1.0  202.792759  240.579208  224.813230  217.643590  221.416667   \n",
       "2023-08   1.0  268.022330  236.717822  228.869619  244.555891         NaN   \n",
       "2023-09   1.0  251.884406  221.119259  285.954686         NaN         NaN   \n",
       "2023-10   1.0  202.535310  275.238938         NaN         NaN         NaN   \n",
       "2023-11   1.0  253.185057         NaN         NaN         NaN         NaN   \n",
       "\n",
       "                +6月         +7月         +8月         +9月        +10月  \\\n",
       "2023-01  227.301840  265.751361  304.673233  287.488854  274.018112   \n",
       "2023-02  244.220820  289.434457  289.619512  258.003289  250.589286   \n",
       "2023-03  294.317889  263.633117  259.095865  256.350000         NaN   \n",
       "2023-04  259.598784  273.954373  239.771930         NaN         NaN   \n",
       "2023-05  257.692443  236.570707         NaN         NaN         NaN   \n",
       "2023-06  217.768145         NaN         NaN         NaN         NaN   \n",
       "2023-07         NaN         NaN         NaN         NaN         NaN   \n",
       "2023-08         NaN         NaN         NaN         NaN         NaN   \n",
       "2023-09         NaN         NaN         NaN         NaN         NaN   \n",
       "2023-10         NaN         NaN         NaN         NaN         NaN   \n",
       "2023-11         NaN         NaN         NaN         NaN         NaN   \n",
       "\n",
       "               +11月  \n",
       "2023-01  250.854839  \n",
       "2023-02         NaN  \n",
       "2023-03         NaN  \n",
       "2023-04         NaN  \n",
       "2023-05         NaN  \n",
       "2023-06         NaN  \n",
       "2023-07         NaN  \n",
       "2023-08         NaN  \n",
       "2023-09         NaN  \n",
       "2023-10         NaN  \n",
       "2023-11         NaN  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>当月新增</th>\n",
       "      <th>+1月</th>\n",
       "      <th>+2月</th>\n",
       "      <th>+3月</th>\n",
       "      <th>+4月</th>\n",
       "      <th>+5月</th>\n",
       "      <th>+6月</th>\n",
       "      <th>+7月</th>\n",
       "      <th>+8月</th>\n",
       "      <th>+9月</th>\n",
       "      <th>+10月</th>\n",
       "      <th>+11月</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01</th>\n",
       "      <td>1.0</td>\n",
       "      <td>265.336824</td>\n",
       "      <td>296.076827</td>\n",
       "      <td>305.718095</td>\n",
       "      <td>279.390176</td>\n",
       "      <td>323.637461</td>\n",
       "      <td>227.301840</td>\n",
       "      <td>265.751361</td>\n",
       "      <td>304.673233</td>\n",
       "      <td>287.488854</td>\n",
       "      <td>274.018112</td>\n",
       "      <td>250.854839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-02</th>\n",
       "      <td>1.0</td>\n",
       "      <td>271.197133</td>\n",
       "      <td>277.464706</td>\n",
       "      <td>279.532982</td>\n",
       "      <td>323.596252</td>\n",
       "      <td>190.416382</td>\n",
       "      <td>244.220820</td>\n",
       "      <td>289.434457</td>\n",
       "      <td>289.619512</td>\n",
       "      <td>258.003289</td>\n",
       "      <td>250.589286</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-03</th>\n",
       "      <td>1.0</td>\n",
       "      <td>246.537415</td>\n",
       "      <td>230.943182</td>\n",
       "      <td>286.855587</td>\n",
       "      <td>202.798498</td>\n",
       "      <td>247.584302</td>\n",
       "      <td>294.317889</td>\n",
       "      <td>263.633117</td>\n",
       "      <td>259.095865</td>\n",
       "      <td>256.350000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04</th>\n",
       "      <td>1.0</td>\n",
       "      <td>216.682367</td>\n",
       "      <td>268.118377</td>\n",
       "      <td>206.103586</td>\n",
       "      <td>231.559871</td>\n",
       "      <td>293.585062</td>\n",
       "      <td>259.598784</td>\n",
       "      <td>273.954373</td>\n",
       "      <td>239.771930</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-05</th>\n",
       "      <td>1.0</td>\n",
       "      <td>251.352381</td>\n",
       "      <td>205.153355</td>\n",
       "      <td>214.200803</td>\n",
       "      <td>267.116379</td>\n",
       "      <td>241.482143</td>\n",
       "      <td>257.692443</td>\n",
       "      <td>236.570707</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06</th>\n",
       "      <td>1.0</td>\n",
       "      <td>190.662222</td>\n",
       "      <td>216.175775</td>\n",
       "      <td>270.653079</td>\n",
       "      <td>253.154930</td>\n",
       "      <td>248.992578</td>\n",
       "      <td>217.768145</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07</th>\n",
       "      <td>1.0</td>\n",
       "      <td>202.792759</td>\n",
       "      <td>240.579208</td>\n",
       "      <td>224.813230</td>\n",
       "      <td>217.643590</td>\n",
       "      <td>221.416667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-08</th>\n",
       "      <td>1.0</td>\n",
       "      <td>268.022330</td>\n",
       "      <td>236.717822</td>\n",
       "      <td>228.869619</td>\n",
       "      <td>244.555891</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-09</th>\n",
       "      <td>1.0</td>\n",
       "      <td>251.884406</td>\n",
       "      <td>221.119259</td>\n",
       "      <td>285.954686</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-10</th>\n",
       "      <td>1.0</td>\n",
       "      <td>202.535310</td>\n",
       "      <td>275.238938</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-11</th>\n",
       "      <td>1.0</td>\n",
       "      <td>253.185057</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 91
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-25T06:57:56.441376Z",
     "start_time": "2024-06-25T06:57:56.420426Z"
    }
   },
   "cell_type": "code",
   "source": "final_df",
   "id": "780b17aad945677e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "          当月新增   +1月     +2月     +3月     +4月     +5月    +6月     +7月     +8月  \\\n",
       "2023-01   8193   573  1601.0  1050.0  1079.0  1906.0  815.0  1102.0   863.0   \n",
       "2023-02   2740   558   340.0   379.0   587.0   293.0  317.0   267.0   205.0   \n",
       "2023-03   8753  1176  1232.0  2112.0   799.0  1032.0  777.0   616.0  1064.0   \n",
       "2023-04   5859   828  1208.0   502.0   618.0   482.0  329.0   526.0   171.0   \n",
       "2023-05   6912  1575   626.0   747.0   464.0   392.0  569.0   198.0     0.0   \n",
       "2023-06  16458  1575  1775.0  1153.0   923.0  1482.0  496.0     0.0     0.0   \n",
       "2023-07   6514   801   404.0   257.0   390.0   144.0    0.0     0.0     0.0   \n",
       "2023-08  11781  1030   606.0   813.0   331.0     0.0    0.0     0.0     0.0   \n",
       "2023-09  30214  2206  2482.0   971.0     0.0     0.0    0.0     0.0     0.0   \n",
       "2023-10  11253  1147   452.0     0.0     0.0     0.0    0.0     0.0     0.0   \n",
       "2023-11  18540   870     0.0     0.0     0.0     0.0    0.0     0.0     0.0   \n",
       "\n",
       "           +9月    +10月   +11月  \n",
       "2023-01  628.0  1049.0  372.0  \n",
       "2023-02  304.0   112.0    0.0  \n",
       "2023-03  360.0     0.0    0.0  \n",
       "2023-04    0.0     0.0    0.0  \n",
       "2023-05    0.0     0.0    0.0  \n",
       "2023-06    0.0     0.0    0.0  \n",
       "2023-07    0.0     0.0    0.0  \n",
       "2023-08    0.0     0.0    0.0  \n",
       "2023-09    0.0     0.0    0.0  \n",
       "2023-10    0.0     0.0    0.0  \n",
       "2023-11    0.0     0.0    0.0  "
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>当月新增</th>\n",
       "      <th>+1月</th>\n",
       "      <th>+2月</th>\n",
       "      <th>+3月</th>\n",
       "      <th>+4月</th>\n",
       "      <th>+5月</th>\n",
       "      <th>+6月</th>\n",
       "      <th>+7月</th>\n",
       "      <th>+8月</th>\n",
       "      <th>+9月</th>\n",
       "      <th>+10月</th>\n",
       "      <th>+11月</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-01</th>\n",
       "      <td>8193</td>\n",
       "      <td>573</td>\n",
       "      <td>1601.0</td>\n",
       "      <td>1050.0</td>\n",
       "      <td>1079.0</td>\n",
       "      <td>1906.0</td>\n",
       "      <td>815.0</td>\n",
       "      <td>1102.0</td>\n",
       "      <td>863.0</td>\n",
       "      <td>628.0</td>\n",
       "      <td>1049.0</td>\n",
       "      <td>372.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-02</th>\n",
       "      <td>2740</td>\n",
       "      <td>558</td>\n",
       "      <td>340.0</td>\n",
       "      <td>379.0</td>\n",
       "      <td>587.0</td>\n",
       "      <td>293.0</td>\n",
       "      <td>317.0</td>\n",
       "      <td>267.0</td>\n",
       "      <td>205.0</td>\n",
       "      <td>304.0</td>\n",
       "      <td>112.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-03</th>\n",
       "      <td>8753</td>\n",
       "      <td>1176</td>\n",
       "      <td>1232.0</td>\n",
       "      <td>2112.0</td>\n",
       "      <td>799.0</td>\n",
       "      <td>1032.0</td>\n",
       "      <td>777.0</td>\n",
       "      <td>616.0</td>\n",
       "      <td>1064.0</td>\n",
       "      <td>360.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-04</th>\n",
       "      <td>5859</td>\n",
       "      <td>828</td>\n",
       "      <td>1208.0</td>\n",
       "      <td>502.0</td>\n",
       "      <td>618.0</td>\n",
       "      <td>482.0</td>\n",
       "      <td>329.0</td>\n",
       "      <td>526.0</td>\n",
       "      <td>171.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-05</th>\n",
       "      <td>6912</td>\n",
       "      <td>1575</td>\n",
       "      <td>626.0</td>\n",
       "      <td>747.0</td>\n",
       "      <td>464.0</td>\n",
       "      <td>392.0</td>\n",
       "      <td>569.0</td>\n",
       "      <td>198.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06</th>\n",
       "      <td>16458</td>\n",
       "      <td>1575</td>\n",
       "      <td>1775.0</td>\n",
       "      <td>1153.0</td>\n",
       "      <td>923.0</td>\n",
       "      <td>1482.0</td>\n",
       "      <td>496.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-07</th>\n",
       "      <td>6514</td>\n",
       "      <td>801</td>\n",
       "      <td>404.0</td>\n",
       "      <td>257.0</td>\n",
       "      <td>390.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-08</th>\n",
       "      <td>11781</td>\n",
       "      <td>1030</td>\n",
       "      <td>606.0</td>\n",
       "      <td>813.0</td>\n",
       "      <td>331.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-09</th>\n",
       "      <td>30214</td>\n",
       "      <td>2206</td>\n",
       "      <td>2482.0</td>\n",
       "      <td>971.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-10</th>\n",
       "      <td>11253</td>\n",
       "      <td>1147</td>\n",
       "      <td>452.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-11</th>\n",
       "      <td>18540</td>\n",
       "      <td>870</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 87
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
